import { Controller } from 'egg';
import * as blue from 'bluebird';
import * as Excel from 'exceljs';


export default class HomeController extends Controller { 

  /**
   * 2020年09月25日02:23:53
   * 算实时转写打印客户名
   */
  public async testTemp() {
    const list = await this.app.commonDB.db('cc').collection('app_translation_config')
      .find({sumAgent: {$gt: 0}}, {fields: {account: 1, sumAgent: 1}}).toArray();
    await blue.map(list, async (doc) => {
      const accountDoc = await this.app.commonDB.db('bill').collection('platform_account').findOne({_id: doc.account});
      doc.companyName = accountDoc?.companyName;
    });
    const filename = `./logs/temp-${Date.now()}.xlsx`;
    const options = {
      filename,
      useStyles: true,
      useSharedStrings: true
    };
    const workbook = new Excel.stream.xlsx.WorkbookWriter(options);
    // const workbook = new Excel.Workbook(options);

    // sheet1
    const sheet = workbook.addWorksheet('阿里云');
    sheet.columns = [
      { key: 'account', header: '账户id' } as any,
      { key: 'companyName', header: '账户名' } as any,
      { key: 'sumAgent', header: '坐席数' } as any,
    ];
    list.forEach((doc) => {
      sheet.addRow(doc).commit();
    });
    await workbook.commit();
    this.ctx.logger.info('打印成功');
    this.ctx.sendSuccess();
  }
}
